package com.dha.asynctask;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.dha.adapter.ThongBaoAdapter;
import com.dha.caodangcongnghe.R;
import com.dha.caodangcongnghe.XemThongBao;
import com.dha.caodangcongnghe.XemThongBaoChiTiet;
import com.dha.model.ThongBaoModel;
import com.dha.variable.ThongBaoVariable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

public class XemThongBaoChiTietAsyncTask extends AsyncTask<Void, String, Boolean> {
	private ProgressDialog dialog;

	private Activity activity;

	private ListView listView;

	private int maThongBao = -1;
	// private ArrayList<ThongBaoModel> arr;

	// private ThongBaoAdapter adapter;

	// private int maThongBao = -1;
	// cái này để chuyển từ dạng "yyyy-MM-dd'T'HH:mm:ss" bên sql qua
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd'T'HH:mm:ss");
	// // cái này để chuyển sang dạng mong muốn
	private SimpleDateFormat dateOutput = new SimpleDateFormat(
			"HH:mm - dd/MM/yyyy");

	public XemThongBaoChiTietAsyncTask(Activity activity, int maThongBao) {
		super();
		this.activity = activity;
		listView = (ListView) activity.findViewById(R.id.lv_ThongBao);
		this.maThongBao = maThongBao;
		// arr = new ArrayList<ThongBaoModel>();

		// adapter = new ThongBaoAdapter(activity,
		// R.layout.layout_thongbao_custom, arr);
		//
		// listView.setAdapter(adapter);

		// this.maThongBao = maThongBao;

		// arr = XemThongBao.arr;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang tải thông báo về");
		super.onPreExecute();
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		String url = ThongBaoVariable.URL;
		String namespace = ThongBaoVariable.NAME_SPACE;
		String methodname = ThongBaoVariable.METHOD_NAME_XEM_THONG_BAO_THEO_MA_THONG_BAO;
		SoapSerializationEnvelope envelope = null;
		try {

			String soapAction = namespace + methodname;
			SoapObject request = new SoapObject(namespace, methodname);
			// doi so ma giao vien
			PropertyInfo maThongBaoInfo = new PropertyInfo();
			maThongBaoInfo.setName("MaThongBao");
			maThongBaoInfo.setValue(maThongBao);
			request.addProperty(maThongBaoInfo);
			//
			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);
			HttpTransportSE httpTransportSE = new HttpTransportSE(url);
			httpTransportSE.call(soapAction, envelope);
			SoapObject so = (SoapObject) envelope.getResponse();

			publishProgress(so.getProperty("tieuDe").toString(), so
					.getProperty("noiDung").toString(),
					so.getProperty("tenGiaoVien").toString(),
					so.getProperty("ngayDang").toString(),
					so.getProperty("maThongBao").toString());

			return true;

		} catch (Exception e) {
			Log.i("hehe", "" + e.toString());
			return false;
		}

	}

	@Override
	protected void onProgressUpdate(String... values) {
		
		XemThongBaoChiTiet.arr.clear();
		
		String tieuDe = values[0].toString();
		String noiDung = values[1].toString();
		String nguoiDang = values[2].toString();
		String ngayDangTuSQL = values[3].toString();
		int maThongBao = Integer.parseInt(values[4].toString());
		String ngayDangInRa = null;
		try {
			// Chuyển dạng tring lấy ra quên sql, service qua dạng ngày
			Date date = dateFormat.parse(ngayDangTuSQL.substring(0, 19));
			// chuyển ngược về lại dạng mong muốn
			ngayDangInRa = dateOutput.format(date);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
		}
		
		ThongBaoModel tb = new ThongBaoModel(maThongBao, tieuDe, noiDung,
				nguoiDang, ngayDangInRa);

		XemThongBaoChiTiet.arr.add(tb);

		super.onProgressUpdate(values);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// TODO Auto-generated method stub
		if (result == false) {
			Toast.makeText(activity, "Không có thông báo", Toast.LENGTH_LONG)
					.show();
		}
		XemThongBaoChiTiet.adapter.notifyDataSetChanged();
		dialog.dismiss();
		super.onPostExecute(result);
	}

}
